﻿using System;
using System.Collections.Generic;
using System.Linq;
using AMM.Business;
using AMM.Business.Business;
using AMM.Business.Config;
using AMM.Business.Project;
using AMM.Model;
using AMM.Model.Business;
using AMM.Model.Project;

namespace AMM.UserControls {
    public partial class ProduceConductEditControl : BaseEditControl {

        private bool _isNew;
        private List<ProduceConduct> _produceConductList;

        public override object BaseEditModel {
            get { return this._produceConductList; }
            set { this._produceConductList = (List<ProduceConduct>)value; }
        }

        public ProduceConductEditControl() {
            this.InitializeComponent();
            this.dgvEdit.AutoGenerateColumns = false;
        }

        private void ProduceConductEditControl_Load(object sender, EventArgs e) {
            this.BindProduceConductType();
            this.BindProduct();
            this.BindCustomerOrder();

            this.BindDataGridView();
            this.CreateFormBindings();
            this.BindValidation();

            this._isNew = false;
            this.dgvEdit.ContextMenuStrip = null;
        }

        private void BindDataGridView() {
            this.dgvEdit.DataSourceEx = this._produceConductList;
            if (!this._isNew && this._produceConductList != null && this._produceConductList.Count > 0) {
                this.ddlCustomerOrder.Enabled = false;
                this.ddlCustomerOrder.SelectedValueEx = this._produceConductList[0].CustomerOrderId;
            }
        }

        private void BindProduceConductType() {
            this.ProduceConductType.DataSource = new BasicsProcessor().FetchEnabledBasicsByType(BasicsType.ProduceConductType);
            this.ProduceConductType.DisplayMember = "Name";
            this.ProduceConductType.ValueMember = "BasicsId";
        }

        private void BindCustomerOrder() {
            CustomerOrderSearchModel searchModel = new CustomerOrderSearchModel();
            CustomerOrderProcessor customerProcessor = new CustomerOrderProcessor(int.MaxValue);
            List<CustomerOrderViewModel> customerList = customerProcessor.FetchCustomerOrderViewModelList(searchModel).ToList();
            List<ProduceConductViewModel> produceConductList = new ProduceConductProcessor().FetchProduceConductViewModelList(new ProduceConductSearchModel());
            customerList = customerList.Where(x => !produceConductList.Any(y => y.CustomerOrderCode == x.OrderNo)).ToList();

            this.ddlCustomerOrder.DisplayMemberEx = "OrderNo";
            this.ddlCustomerOrder.ValueMemberEx = "Key";
            this.ddlCustomerOrder.HeightEx = 300;
            this.ddlCustomerOrder.WidthEx = 400;
            this.ddlCustomerOrder.PageSizeEx = 20;
            this.ddlCustomerOrder.DataBind(new CustomerOrderViewModel(), x => new { x.OrderNo, x.CustomerCode, x.CustomerName }, customerList);
        }

        private void BindProduct() {
            ProductSearchModel searchModel = new ProductSearchModel() { IsEnabled = true };
            ProductProcessor productProcessor = new ProductProcessor(int.MaxValue);
            List<ProductViewModel> productList = productProcessor.FetchProductViewModelList(searchModel);

            this.Product.DisplayMemberEx = "Code";
            this.Product.ValueMemberEx = "Key";
            this.Product.HeightEx = 300;
            this.Product.WidthEx = 400;
            this.Product.PageSizeEx = 20;
            this.Product.DataBind(new ProductViewModel(), x => new { x.ProductType, x.Code, x.Name, x.Remarks }, productList);
        }

        private void CreateFormBindings() {
            /*
            this._produceConductList.CreateBinding(this.ddlCustomerOrder, x => x.SelectedValueEx, y => y.CustomerOrderId);
            this._fbs.CreateBinding(this.tbProduceConduct, x => x.Text, y => y.Price);
            this._fbs.CreateBinding(this.tbProcurementCycle, x => x.Text, y => y.ProcurementCycle);
            this._fbs.CreateBinding(this.tbBatchAmount, x => x.Text, y => y.PreProcurementCycle);
            this._fbs.CreateBinding(this.cbIsMultiple, x => x.Checked, y => y.IsMultiple);
            this._fbs.CreateBinding(this.tbMinimumPackage, x => x.Text, y => y.MinimumPackage);
            this._fbs.CreateBinding(this.tbMinimumProcurement, x => x.Text, y => y.MinimumProcurement);
            this._fbs.CreateBinding(this.cmbProduceConductType, x => x.SelectedValue, y => y.UnitId);
            this._fbs.CreateBinding(this.ddlMaterial, x => x.SelectedValueEx, y => y.MaterialId);
            this._fbs.CreateBinding(this.attachment, x => x.Text, y => y.AttachmentId, DataSourceUpdateMode.OnPropertyChanged);
            */
        }

        private void BindValidation() {
            /*
            this.ValidatorForEdit.SetType(this.tbProduceConduct, ValidationType.Required | ValidationType.RegularExpression);
            this.ValidatorForEdit.SetRequiredMessage(this.tbProduceConduct, this.RequiredMessage);
            this.ValidatorForEdit.SetRegularExpression(this.tbProduceConduct, "^([0-9]+.)?[0-9]+$");
            this.ValidatorForEdit.SetRegularExpressionMessage(this.tbProduceConduct, "单价只能由数字组成");

            this.ValidatorForEdit.SetType(this.ddlCustomerOrder, ValidationType.Required);
            this.ValidatorForEdit.SetRequiredMessage(this.ddlCustomerOrder, this.RequiredMessage);

            this.ValidatorForEdit.SetType(this.ddlMaterial, ValidationType.Required);
            this.ValidatorForEdit.SetRequiredMessage(this.ddlMaterial, this.RequiredMessage);

            this.ValidatorForEdit.SetType(this.cmbProduceConductType, ValidationType.Required);
            this.ValidatorForEdit.SetRequiredMessage(this.cmbProduceConductType, this.RequiredMessage);

            this.ValidatorForEdit.SetType(this.tbProcurementCycle, ValidationType.Required | ValidationType.RegularExpression);
            this.ValidatorForEdit.SetRequiredMessage(this.tbProcurementCycle, this.RequiredMessage);
            this.ValidatorForEdit.SetRegularExpression(this.tbProcurementCycle, "^[0-9-]+$");
            this.ValidatorForEdit.SetRegularExpressionMessage(this.tbProcurementCycle, "采购周期只能由数字组成");

            this.ValidatorForEdit.SetType(this.tbBatchAmount, ValidationType.Required | ValidationType.RegularExpression);
            this.ValidatorForEdit.SetRequiredMessage(this.tbBatchAmount, this.RequiredMessage);
            this.ValidatorForEdit.SetRegularExpression(this.tbBatchAmount, "^[0-9-]+$");
            this.ValidatorForEdit.SetRegularExpressionMessage(this.tbBatchAmount, "采购前置周期只能由数字组成");

            this.ValidatorForEdit.SetType(this.tbMinimumPackage, ValidationType.Required | ValidationType.RegularExpression);
            this.ValidatorForEdit.SetRequiredMessage(this.tbMinimumPackage, this.RequiredMessage);
            this.ValidatorForEdit.SetRegularExpression(this.tbMinimumPackage, "^[0-9-]+$");
            this.ValidatorForEdit.SetRegularExpressionMessage(this.tbMinimumPackage, "最小包装只能由数字组成");

            this.ValidatorForEdit.SetType(this.tbMinimumProcurement, ValidationType.Required | ValidationType.RegularExpression);
            this.ValidatorForEdit.SetRequiredMessage(this.tbMinimumProcurement, this.RequiredMessage);
            this.ValidatorForEdit.SetRegularExpression(this.tbMinimumProcurement, "^[0-9-]+$");
            this.ValidatorForEdit.SetRegularExpressionMessage(this.tbMinimumProcurement, "最低采购量只能由数字组成");
            */
        }

        private void ddlCustomerOrder_ValueChanged(object sender, EventArgs e) {
            if (this.ddlCustomerOrder.SelectedValueEx == null) { return; }
            string autoCode = CommonHelper.GenerateAutoCode("W");
            int i = new ProduceConductProcessor().GetTheMaxFlowNumber(autoCode);
            int customerOrderId = Convert.ToInt32(this.ddlCustomerOrder.SelectedValueEx);
            CustomerOrder customerOrder = new CustomerOrderProcessor().FetchCustomerOrderById(customerOrderId);
            List<ProduceConduct> pcList = new List<ProduceConduct>();
            customerOrder.CustomerOrderDetails.ToList().ForEach(x => {
                pcList.Add(new ProduceConduct() {
                    ProduceConductId = Guid.NewGuid().GetHashCode(),
                    CustomerOrderId = x.CustomerOrderId,
                    ProductId = x.ProductId,
                    Code = CommonHelper.GendrateSpecificCode(autoCode, i),
                    OrderDate = DateTime.Now.Date,
                    BatchAmount = x.Quantity,
                    ClassStatus = ClassStatus.Added
                });
                i++;
            });

            this._isNew = true;
            this._produceConductList = pcList;
            this.BindDataGridView();
        }
    }
}
